package com.study.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @description: 文件操作工具类
 * @author: LChuang
 * @date: 2023/10/7 10:14
 */
@Slf4j
public class FileUtil {

    /**
     * 文件转换，MultipartFile-->File
     */
    public static File multipartFileToFile(MultipartFile multiFile) {
        String fileName = multiFile.getOriginalFilename();
        assert fileName != null;
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        try {
            File file = File.createTempFile("annex", prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            log.error("MultipartFile文件转换异常：{}", e.getMessage());
        }
        return null;
    }

}
